package com.corpgovernment.travelstandard.impl;

import com.corpgovernment.common.utils.MybatisBatchUtils;
import com.corpgovernment.travelstandard.entity.db.MbTravelStandCarAddress;
import com.corpgovernment.travelstandard.mapper.MbTravelStandCarAddressMapper;
import com.corpgovernment.travelstandard.mapper.MbTravelStandCarCrossCityMapper;
import com.ctrip.corp.obt.generic.utils.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class MbTravelStandCarAddressService {

    @Autowired
    private MbTravelStandCarAddressMapper mbTravelStandCarAddressMapper;

    @Autowired
    private MybatisBatchUtils mybatisBatchUtils;

    public List<MbTravelStandCarAddress> selectByTsid(String tsId) {
        Example example = new Example(MbTravelStandCarAddress.class);
        example.createCriteria().andEqualTo("tsid", tsId);
        return mbTravelStandCarAddressMapper.selectByExample(example);
    }

    public List<MbTravelStandCarAddress> selectByTsids(List<Long> tsIds) {
        Example example = new Example(MbTravelStandCarAddress.class);
        example.createCriteria().andIn("tsid", tsIds);
        return mbTravelStandCarAddressMapper.selectByExample(example);
    }

    public boolean saveBatch(List<MbTravelStandCarAddress> carAddressList) {
        if (CollectionUtils.isEmpty(carAddressList)) {
            return false;
        }
        long result = mybatisBatchUtils.batchUpdateOrInsert(carAddressList, MbTravelStandCarAddressMapper.class, (item, mbTravelStandCarAddressMapper) -> mbTravelStandCarAddressMapper.insertSelective(item));
        return result > 0;
    }

    public boolean delByTsid(String tsId) {
        Example example = new Example(MbTravelStandCarAddress.class);
        example.createCriteria().andEqualTo("tsid", tsId);
        int result = mbTravelStandCarAddressMapper.deleteByExample(example);
        return result > 0;
    }
}
